﻿using System;
using System.IO;
using System.Net;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace LenderClearingHouse
{
  public static class RequestClient
  {
    /// <summary>
    /// Make HTTP query to desired URL.
    /// </summary>
    /// <param name="url">Target query URL.</param>
    /// <returns>Content of HTTP response.</returns>
    /// <exception cref="System.InvalidOperationException" />
    public static string Get(string url)
    {
      return RequestClient.Get(url, Encoding.Default);
    }
    /// <summary>
    /// Make HTTP query to desired URL.
    /// </summary>
    /// <param name="url">Target query URL.</param>
    /// <param name="encoding">Encoding of requested data.</param>
    /// <returns>Content of HTTP response.</returns>
    /// <exception cref="System.InvalidOperationException" />
    public static string Get(string url, Encoding encoding)
    {
      return RequestClient.Get(url, encoding, false);
    }
    /// <summary>
    /// Make HTTP query to desired URL.
    /// </summary>
    /// <param name="url">Target query URL.</param>
    /// <param name="encoding">Encoding of requested data.</param>
    /// <param name="automatic">Redirect automatically if it is necessary.</param>
    /// <returns>Content of HTTP response.</returns>
    /// <exception cref="System.InvalidOperationException" />
    public static string Get(string url, Encoding encoding, bool automatic)
    {
      return RequestClient.Inquire(url, "GET", encoding, automatic);
    }

    #region Assistants
    /// <summary>
    /// Make HTTP query to desired URL.
    /// </summary>
    /// <param name="url">Target query URL.</param>
    /// <param name="method">One of GET/POST/PUT/DELETE.</param>
    /// <param name="encoding">Encoding of requested data.</param>
    /// <param name="automatic">Redirect automatically if it is necessary.</param>
    /// <returns>Content of HTTP response.</returns>
    /// <exception cref="System.InvalidOperationException" />
    private static string Inquire(string url, string method, Encoding encoding, bool automatic)
    {
      try
      {
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
        request.Method = method;
        request.AllowAutoRedirect = automatic;
        request.CookieContainer = new CookieContainer();
        request.ContentLength = 0;

        WebResponse response = request.GetResponse();
        Stream stream = response.GetResponseStream();
        StreamReader reader = new StreamReader(stream, encoding);

        string data = reader.ReadToEnd();

        reader.Close();
        stream.Close();
        response.Close();

        return data;
      }
      catch (Exception exception)
      {
        throw new InvalidOperationException(
            string.Format("Failed to query {0}. See inner exception for details.", url), exception);
      }
    }
    #endregion
  }
}